/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MasterSupplier.java
 *
 * Created on Aug 9, 2010, 9:39:41 PM
 */
package com.ivanbiz.gui;

import com.ivanbiz.helper.CenterRenderer;
import com.ivanbiz.dao.SatuanDAO;
import com.ivanbiz.impl.SatuanDAOImpl;
import com.ivanbiz.model.Satuan;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

/**
 *
 * @author Aprisma
 */
public class MasterSatuan extends javax.swing.JInternalFrame {

    SatuanDAO satuanDAO = new SatuanDAOImpl();
    List<Satuan> listSatuan = new ArrayList<Satuan>();

    /** Creates new form MasterSupplier */
    public MasterSatuan() {
        initComponents();
        updateData();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        paneCustomer = new javax.swing.JPanel();
        labelKode = new javax.swing.JLabel();
        labelNama = new javax.swing.JLabel();
        textKode = new javax.swing.JTextField();
        textNama = new javax.swing.JTextField();
        panelTombol = new javax.swing.JPanel();
        buttonSimpan = new javax.swing.JButton();
        buttonHapus = new javax.swing.JButton();
        panelDaftarCustomer = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableSatuan = new javax.swing.JTable();

        setBorder(new org.jdesktop.swingx.border.DropShadowBorder());
        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setTitle("Master Satuan");

        paneCustomer.setBorder(javax.swing.BorderFactory.createTitledBorder("Satuan"));

        labelKode.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelKode.setText("Kode");

        labelNama.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelNama.setText("Nama");

        textKode.setFont(new java.awt.Font("Tahoma", 1, 11));

        textNama.setFont(new java.awt.Font("Tahoma", 1, 11));

        javax.swing.GroupLayout paneCustomerLayout = new javax.swing.GroupLayout(paneCustomer);
        paneCustomer.setLayout(paneCustomerLayout);
        paneCustomerLayout.setHorizontalGroup(
            paneCustomerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(paneCustomerLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(paneCustomerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelNama)
                    .addComponent(labelKode))
                .addGap(15, 15, 15)
                .addGroup(paneCustomerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(textNama, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 568, Short.MAX_VALUE)
                    .addComponent(textKode, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 568, Short.MAX_VALUE))
                .addContainerGap())
        );
        paneCustomerLayout.setVerticalGroup(
            paneCustomerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(paneCustomerLayout.createSequentialGroup()
                .addGroup(paneCustomerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textKode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelKode))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(paneCustomerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textNama, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelNama))
                .addContainerGap(17, Short.MAX_VALUE))
        );

        panelTombol.setBorder(javax.swing.BorderFactory.createTitledBorder("Tombol"));

        buttonSimpan.setFont(new java.awt.Font("Tahoma", 1, 11));
        buttonSimpan.setText("Simpan");
        buttonSimpan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonSimpanActionPerformed(evt);
            }
        });

        buttonHapus.setFont(new java.awt.Font("Tahoma", 1, 11));
        buttonHapus.setText("Hapus");
        buttonHapus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonHapusActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelTombolLayout = new javax.swing.GroupLayout(panelTombol);
        panelTombol.setLayout(panelTombolLayout);
        panelTombolLayout.setHorizontalGroup(
            panelTombolLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelTombolLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelTombolLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(buttonSimpan)
                    .addComponent(buttonHapus, javax.swing.GroupLayout.DEFAULT_SIZE, 77, Short.MAX_VALUE))
                .addContainerGap())
        );
        panelTombolLayout.setVerticalGroup(
            panelTombolLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelTombolLayout.createSequentialGroup()
                .addComponent(buttonSimpan)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonHapus)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        panelDaftarCustomer.setBorder(javax.swing.BorderFactory.createTitledBorder("Daftar Satuan"));

        tableSatuan.setAutoCreateRowSorter(true);
        tableSatuan.setFont(new java.awt.Font("Tahoma", 1, 11));
        tableSatuan.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tableSatuan.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
        tableSatuan.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tableSatuanMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tableSatuan);

        javax.swing.GroupLayout panelDaftarCustomerLayout = new javax.swing.GroupLayout(panelDaftarCustomer);
        panelDaftarCustomer.setLayout(panelDaftarCustomerLayout);
        panelDaftarCustomerLayout.setHorizontalGroup(
            panelDaftarCustomerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelDaftarCustomerLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 734, Short.MAX_VALUE)
                .addContainerGap())
        );
        panelDaftarCustomerLayout.setVerticalGroup(
            panelDaftarCustomerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelDaftarCustomerLayout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 364, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(panelDaftarCustomer, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(paneCustomer, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(panelTombol, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(paneCustomer, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(panelTombol, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panelDaftarCustomer, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-800)/2, (screenSize.height-550)/2, 800, 550);
    }// </editor-fold>//GEN-END:initComponents

    private void buttonHapusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonHapusActionPerformed
        try {
            Satuan satuan = (Satuan) listSatuan.get(tableSatuan.getSelectedRow());
            satuanDAO.delete(satuan);
            updateData();
        } catch (Exception ex) {
            Logger.getLogger(MasterSatuan.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_buttonHapusActionPerformed

    private void buttonSimpanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSimpanActionPerformed
        try {
            if (textKode.getText().equals("") || textNama.getText().equals("")) {
                JOptionPane.showMessageDialog(null, "Data belum Lengkap !!!");
            } else {
                Satuan satuans = (Satuan) satuanDAO.getDataByEqual(Satuan.class, "kode", textKode.getText().toUpperCase());
                if (satuans != null) {
                    JOptionPane.showMessageDialog(null, "Kode Sudah Ada!!!");
                } else {
                    Satuan satuan = new Satuan();
                    satuan.setKode(textKode.getText().toUpperCase());
                    satuan.setNama(textNama.getText().toUpperCase());
                    satuan.setCreateDate(new Date());
                    satuanDAO.save(satuan);
                    updateData();
                }
            }
        } catch (Exception ex) {
            Logger.getLogger(MasterSatuan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_buttonSimpanActionPerformed

    private void tableSatuanMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tableSatuanMouseClicked
        Satuan satuan = (Satuan) listSatuan.get(tableSatuan.getSelectedRow());
        textKode.setText(satuan.getKode());
        textNama.setText(satuan.getNama());
        buttonHapus.setEnabled(true);
        buttonSimpan.setEnabled(false);
    }//GEN-LAST:event_tableSatuanMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonHapus;
    private javax.swing.JButton buttonSimpan;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel labelKode;
    private javax.swing.JLabel labelNama;
    private javax.swing.JPanel paneCustomer;
    private javax.swing.JPanel panelDaftarCustomer;
    private javax.swing.JPanel panelTombol;
    private javax.swing.JTable tableSatuan;
    private javax.swing.JTextField textKode;
    private javax.swing.JTextField textNama;
    // End of variables declaration//GEN-END:variables

    private void updateData() {
        try {
            buttonSimpan.setEnabled(true);
            buttonHapus.setEnabled(false);
            textKode.setText("");
            textNama.setText("");
            listSatuan = satuanDAO.getAll(Satuan.class);
            updateTabelSatuan();
        } catch (Exception ex) {

            Logger.getLogger(MasterSatuan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void updateTabelSatuan() {
        String[] judul = {"No", "Kode Barang", "Nama Barang"};
        Object[][] isi = new Object[listSatuan.size()][3];
        int x = 0;
        int no = 0;
        for (Satuan satuan : listSatuan) {
            isi[x][0] = no += 1;
            isi[x][1] = satuan.getKode();
            isi[x][2] = satuan.getNama();
            x++;
        }

        tableSatuan.setModel(new DefaultTableModel(isi, judul) {

            @Override
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return false;
            }
        });
        TableColumn column = tableSatuan.getColumnModel().getColumn(0);
        column.setPreferredWidth(75);
        column.setMaxWidth(75);
        column.setCellRenderer(new CenterRenderer());
        TableColumn column1 = tableSatuan.getColumnModel().getColumn(1);
        column1.setPreferredWidth(100);
        column1.setMaxWidth(100);
        column1.setCellRenderer(new CenterRenderer());
    }
}
